package com.dysoft.flesh;

/**
 * @author Sean Micklethwaite
 *         Aug 14, 2010 9:59:36 PM
 */
public abstract class AbstractRenderContext<RendererT extends Renderer.Root> implements RenderContext<RendererT> {
	final RendererT renderer;
	RenderTarget target;
	int rendererDepth = 0;
	int transformDepth = 0;

	public AbstractRenderContext(RendererT renderer) {
		this.renderer = renderer;
		target = renderer;
	}

	public RenderTarget getTarget() {
		return target;
	}

	public void pushTarget(RenderTarget target) {
		this.target = target;
	}

	public void popTarget() {
		this.target = renderer;
	}

	public void pushRenderer(Renderer renderer) {
		rendererDepth++;
	}

	public void popRenderer() {
		assert rendererDepth > 0;
		rendererDepth--;
		if(rendererDepth == 0) {
			while(transformDepth > 0)
				popTransform();
		}
	}

	public boolean isRendering() {
		return rendererDepth > 0;
	}

	public void pushTransform() {
		transformDepth++;
	}

	public void popTransform() {
		assert transformDepth > 0;
		transformDepth--;
	}

	public RendererT getRenderer() {
		return renderer;
	}

	public int getTransformDepth() {
		return transformDepth;
	}
}
